// 自定义错误 当捕获到发生的错误时，先抛出自定义的错误

const { formatResponse } = require("./tool")

// 业务处理错误基类
class ServiceError extends Error {
    /**
     * 
     * @param {*} message 错误消息
     * @param {*} code 错误消息码
     */
    constructor(message, code) {
        super(message)
        this.code = code
    }

    // 方法
    toResponseJSON() {
        return formatResponse(this.code, this.message, null)
    }
}

// 文件上传错误
exports.UploadError = class extends ServiceError {
    constructor(message) {
        super(message, 413)
    }
}
// 禁止访问错误
exports.ForbiddenError = class extends ServiceError {
    constructor(message) {
        super(message, 401)
    }
}
// 验证错误
exports.ValidationError = class extends ServiceError {
    constructor(message) {
        super(message, 406)
    }
}
// 无资源错误
exports.NotFoundError = class extends ServiceError {
    constructor(message) {
        super(message, 404)
    }
}
// 未知错误
exports.UnknownError = class extends ServiceError {
    constructor() {
        super("server internal error", 500);
    }
}

module.exports.ServiceError = ServiceError